package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/checkGroup")
@ResponseBody
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;

    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('CHECKGROUP_ADD')")//添加权限限制
    public Result add(@RequestBody CheckGroup checkGroup, Integer[]checkitemIds){
        boolean flag=false;
        String message=null;
        try {
            checkGroupService.add(checkGroup,checkitemIds);
            flag=true;
            message=MessageConstant.ADD_CHECKGROUP_SUCCESS;
        } catch (Exception e) {
            message=MessageConstant.ADD_CHECKGROUP_FAIL;
            e.printStackTrace();
        }
        return new Result(flag,message);
    }

    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")//添加权限限制
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult=null;
        boolean flag=false;
        String message=null;
        try {
            pageResult=checkGroupService.findPage(queryPageBean);
            flag=true;
            message=MessageConstant.QUERY_CHECKGROUP_SUCCESS;
        } catch (Exception e) {
            message=MessageConstant.QUERY_CHECKGROUP_FAIL;
            e.printStackTrace();
        }
        return new Result(flag,message,pageResult);
    }

    @RequestMapping("/findCheckGroupById")
    public Result findCheckGroupById(Integer id){
        CheckGroup checkGroup=null;
        boolean flag=false;
        String message=null;
        try {
            checkGroup=checkGroupService.findCheckGroupById(id);
            flag=true;
            message=MessageConstant.QUERY_CHECKGROUP_SUCCESS;
        } catch (Exception e) {
            message=MessageConstant.QUERY_CHECKGROUP_FAIL;
            e.printStackTrace();
        }
        return new Result(flag,message,checkGroup);
    }

    @RequestMapping("/findCheckGroupId_CheckItemIdById")
    public Result findCheckGroupId_CheckItemIdById(Integer id){
        List<Integer>integerList=null;
        boolean flag=false;
        String message=null;
        try {
            integerList = checkGroupService.findCheckGroupId_CheckItemIdById(id);
            flag=true;
            message="查询检查组绑定检查项成功";
        } catch (Exception e) {
            message="查询检查组绑定检查项失败";
            e.printStackTrace();
        }
        return new Result(flag,message,integerList);
    }

    @RequestMapping("/update")
    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    public Result update(@RequestBody CheckGroup checkGroup,Integer[]checkitemIds){
        boolean flag=false;
        String message=null;
        try {
            checkGroupService.update(checkGroup,checkitemIds);
            flag=true;
            message=MessageConstant.EDIT_CHECKGROUP_SUCCESS;
        } catch (Exception e) {
            message=MessageConstant.EDIT_CHECKGROUP_FAIL;
            e.printStackTrace();
        }
        return new Result(flag,message);
    }

    @RequestMapping("/del")
    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    public Result del(Integer id){
        String message=null;
        boolean flag=false;
        try {
            checkGroupService.del(id);
            flag=true;
            message=MessageConstant.DELETE_CHECKGROUP_SUCCESS;
        } catch (Exception e) {
            message=MessageConstant.DELETE_CHECKGROUP_FAIL;
            e.printStackTrace();
        }
        return new Result(flag,message);
    }

    @RequestMapping("/findAllCheckGroup")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public Result findAll(){
        boolean flag=false;
        String message=null;
        List<CheckGroup> checkGroupList=null;
        try {
            checkGroupList=checkGroupService.findAllCheckGroup();
            flag=true;
            message=MessageConstant.QUERY_CHECKGROUP_SUCCESS;
        } catch (Exception e) {
            message=MessageConstant.QUERY_CHECKGROUP_FAIL;
            e.printStackTrace();
        }
        return new Result(flag,message,checkGroupList);
    }
}
